package com.smartEdu.service.impl.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.CourseDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.CourseDeatailModel;
import com.smartEdu.model.CourseInstructionModel;
import com.smartEdu.model.HomeWorkModel;
import com.smartEdu.service.CourseService;

@Service(value="courseAppServiceImpl")
public class CourseServiceImpl extends BaseServiceImpl implements CourseService{

	@Autowired
	@Qualifier(value="courseAppDaoImpl")
	private CourseDao courseDao;

	@Override
	public void getCourseDetail(Session sess) {
		List<CourseDeatailModel> models=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			models=courseDao.getCourseDetail();
			
			/*
			 * 此处写死测试
			 */
			for (CourseDeatailModel courseDeatailModel : models) {
				if (courseDeatailModel.getNickname()!=null) {
					courseDeatailModel.setTitle(courseDeatailModel.getNickname());
					courseDeatailModel.setName(null);
					courseDeatailModel.setNickname(null);
				}
				else {
					courseDeatailModel.setTitle(courseDeatailModel.getName());
					courseDeatailModel.setName(null);
				}
				if (courseDeatailModel.getPk().equals("AF3442FSAFA")) {
					courseDeatailModel.setWeight((short)2);
				}
			}
			sess.put("course", models);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("CourseDeatail业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("CourseDeatail业务流程出错");
		}
		finally {
			map=null;
		}
	}

	@Override
	public void getCourseDetailByPk(Session sess) {
		// TODO Auto-generated method stub
		List<CourseDeatailModel> models=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			models=courseDao.getCourseDetailByPk(map.gets("pk"));
			sess.put("genre", models);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
			
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("HomeWork业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("HomeWork业务流程出错");
		}
		finally {
			map=null;
		}
	}

	@Override
	public void getCourseInstructionByPk(Session sess) {
		// TODO Auto-generated method stub
		List<CourseInstructionModel> models=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			models=courseDao.getCourseInstructionByPk(map.gets("pk").toString());
			System.out.println(map.gets("pk").toString());
			sess.put("content", models);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
			
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("json出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("业务流程出错");
		}
		finally {
			map=null;
		}
	}

}
